package com.hnluchuan.bmjlearning.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnluchuan.bmjlearning.dao.AgentServicePackageDAO;
import com.hnluchuan.bmjlearning.model.AgentServicePackage;
import com.hnluchuan.bmjlearning.dto.AgentServicePackageDTO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.utils.common.BeanMapper;

@Service
public class AgentServicePackageService extends BaseService {
	
	@Autowired
	private AgentServicePackageDAO agentServicePackageDAO;
	
	public List<AgentServicePackageDTO> find(AgentServicePackageDTO query, Page page) {
		List<AgentServicePackage> models = agentServicePackageDAO.find(query, page);
		List<AgentServicePackageDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<AgentServicePackageDTO> findAll() {
		return this.find(new AgentServicePackageDTO(), null);
	}
	
	public List<AgentServicePackageDTO> findAll(AgentServicePackageDTO query) {
		return this.find(query, null);
	}
	
	public List<AgentServicePackageDTO> find(Page page) {
		return this.find(new AgentServicePackageDTO(), page);
	}
	
	public List<AgentServicePackageDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new AgentServicePackageDTO(), page);
	}

	public List<AgentServicePackageDTO> findByUserId(Long userId){
		List<AgentServicePackage> models = agentServicePackageDAO.findByUserId(userId);
		return toDTOs(models);
	}

	public List<AgentServicePackageDTO> findAllIncludeHistoryByUserId(Long userId){
		List<AgentServicePackage> models = agentServicePackageDAO.findAllIncludeHistoryByUserId(userId);
		return toDTOs(models);
	}

	public AgentServicePackageDTO findByServicePackageIdAndAgentUserId(Long servicePackageId, Long agentUserId){
		AgentServicePackage model = agentServicePackageDAO.findByServicePackageIdAndAgentUserId(servicePackageId, agentUserId);
		return toDTO(model);
	}

	public Double getValidServicePackageCount(Long agentUserId, Long servicePackageId){
		return agentServicePackageDAO.getValidServicePackageCount(agentUserId, servicePackageId);
	}

	public Long create(AgentServicePackageDTO dto) {
		AgentServicePackage model = new AgentServicePackage();
		toModel(model, dto);
		return agentServicePackageDAO.create(model);
	}
	
	public AgentServicePackageDTO load(Long id) {
	    AgentServicePackage model = agentServicePackageDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(AgentServicePackageDTO dto) {
		AgentServicePackage model = agentServicePackageDAO.load(dto.getId());
		toModel(model, dto);
		agentServicePackageDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		throw new RuntimeException("默认禁止删除，请确认是物理删除还是逻辑删除");
		/*
		if (ids != null) {
			for (Long id : ids) {
				agentServicePackageDAO.deleteById(id);
			}
		} */
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public AgentServicePackageDTO findOne(AgentServicePackageDTO query) {
		AgentServicePackage model = agentServicePackageDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<AgentServicePackageDTO> toDTOs(List<AgentServicePackage> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<AgentServicePackageDTO>(0);
		}
		List<AgentServicePackageDTO> dtos = new ArrayList<AgentServicePackageDTO>(models.size());
		for (AgentServicePackage model : models) {
	        AgentServicePackageDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private AgentServicePackageDTO toDTO(AgentServicePackage model) {
		if (model == null) {
			return null;
		}
		AgentServicePackageDTO dto = BeanMapper.map(model, AgentServicePackageDTO.class);
		
		return dto;
	}

	public int deleteAllAgentServicePackageByUserId(Long userId){
		return agentServicePackageDAO.deleteAllAgentServicePackageByUserId(userId);
	}
	
	private void toModel(AgentServicePackage model, AgentServicePackageDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<AgentServicePackage> toModels(List<AgentServicePackageDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<AgentServicePackage>(0);
		}
		List<AgentServicePackage> models = new ArrayList<AgentServicePackage>(dtos.size());
		for (AgentServicePackageDTO dto : dtos) {
	        AgentServicePackage model = new AgentServicePackage();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
